var background = chrome.extension.getBackgroundPage();
var storage = background.storage;
var showNB = false;
var showE = false;
var showLE = false;

// Setup event handlers (Chrome wont let us set them up inline)
initializeFields();

function initializeFields(){
    var notifications = document.getElementById("notifications");
	var clearcache = document.getElementById("ccoe");
    
    notifications.checked = storage.get("notifications");
	clearcache.checked = storage.get("clearcache");
}

document.getElementById("notifications").addEventListener('change', toggleNotifications);

/** This would toggle notifications, if it toggled notifications. */
function toggleNotifications() {
	if(notifications.checked) {
		storage.set("notifications", true);
	} else {
		storage.set("notifications", false);
	}
    console.log("Notifications: " + storage.get("notifications"));
}

document.getElementById("NELabel").addEventListener('click', toggleNotificationBox);

function toggleNotificationBox() {
	if (!showNB) {				
		document.getElementById("NELabel").removeEventListener('click', toggleNotificationBox);
		document.getElementById("NEArea").innerHTML =
			"<label id=\"NELabel\">" +
				"Notification Exceptions*" +
				"<img class=\"ralign\" src=\"minus.png\"" +
					"alt=\"Click to hide notifications exceptions.\" height=\"15\" width=\"17\"/>" +
			"</label>" +
			"<p>List the URL of each site to silence on a separate line.</p>" +
			"<p>List any URLs with \"www\" "
			"<p>These have no effect if notifications are turned off.</p>" +
			"<textarea type=\"text\" id=\"NotificationExceptions\"></textarea>";
		showNB = true;
		document.getElementById("NELabel").addEventListener('click', toggleNotificationBox);
		document.getElementById("NotificationExceptions").value = storage.get("notificationexceptions").join("\n");
		document.getElementById("NotificationExceptions").addEventListener('change', setNotificationExceptions);
	} else {
		document.getElementById("NotificationExceptions").removeEventListener('change', setNotificationExceptions);
		document.getElementById("NELabel").removeEventListener('click', toggleNotificationBox);
		document.getElementById("NEArea").innerHTML =
			"<label id=\"NELabel\">" +
				"Notification Exceptions*" +
				"<img class=\"ralign\" src=\"plus.png\"" +
					"alt=\"Click to show notifications exceptions.\" height=\"15\" width=\"17\"/>" +
			"</label>";
		showNB = false;
		document.getElementById("NELabel").addEventListener('click', toggleNotificationBox);
	}
}

document.getElementById("ELabel").addEventListener('click', toggleExceptionsBox);

function toggleExceptionsBox() {
	if (!showE) {				
		document.getElementById("ELabel").removeEventListener('click', toggleExceptionsBox);
		document.getElementById("EArea").innerHTML =
			"<label id=\"ELabel\">" +
				"Site Exceptions*" +
				"<img class=\"ralign\" src=\"minus.png\"" +
					"alt=\"Click to hide exceptions.\" height=\"15\" width=\"17\"/>" +
			"</label>" +
			"<p>List the URL of each site to ignore on a separate line.</p>" +
			"<textarea type=\"text\" id=\"Exceptions\"></textarea>";
		showE = true;
		document.getElementById("ELabel").addEventListener('click', toggleExceptionsBox);
		document.getElementById("Exceptions").addEventListener('change', setExceptions);
		document.getElementById("Exceptions").value = storage.get("exceptions").join("\n");
	} else {
		document.getElementById("Exceptions").removeEventListener('change', setExceptions);
		document.getElementById("ELabel").removeEventListener('click', toggleExceptionsBox);
		document.getElementById("EArea").innerHTML =
			"<label id=\"ELabel\">" +
				"Site Exceptions*" +
				"<img class=\"ralign\" src=\"plus.png\"" +
					"alt=\"Click to show exceptions.\" height=\"15\" width=\"17\"/>" +
			"</label>";
		showE = false;
		document.getElementById("ELabel").addEventListener('click', toggleExceptionsBox);
	}
}

document.getElementById("LELabel").addEventListener('click', toggleLinkExceptionsBox);

function toggleLinkExceptionsBox() {
	if (!showLE) {
		document.getElementById("LELabel").removeEventListener('click', toggleLinkExceptionsBox);
		document.getElementById("LEArea").innerHTML =
			"<label id=\"LELabel\">" +
				"Link Exceptions*" +
				"<img class=\"ralign\" src=\"minus.png\"" +
					"alt=\"Click to hide exceptions.\" height=\"15\" width=\"17\"/>" +
			"</label>" +
			"<p>List the URL of each link to ignore on a separate line.</p>" +
			"<textarea type=\"text\" id=\"LinkExceptions\"></textarea>";
		showLE = true;
		document.getElementById("LELabel").addEventListener('click', toggleLinkExceptionsBox);
		document.getElementById("LinkExceptions").addEventListener('change', setLinkExceptions);
		document.getElementById("LinkExceptions").value = storage.get("linkexceptions").join("\n");
	} else {
		document.getElementById("LinkExceptions").removeEventListener('change', setLinkExceptions);
		document.getElementById("LELabel").removeEventListener('click', toggleLinkExceptionsBox);
		document.getElementById("LEArea").innerHTML =
			"<label id=\"LELabel\">" +
				"Link Exceptions*" +
				"<img class=\"ralign\" src=\"plus.png\"" +
					"alt=\"Click to show exceptions.\" height=\"15\" width=\"17\"/>" +
			"</label>";
		showLE = false;
		document.getElementById("LELabel").addEventListener('click', toggleLinkExceptionsBox);
	}
}


/** Names pages that specifically won't have notifications, even if they're on. */
function setNotificationExceptions() {
	var exc = document.getElementById("NotificationExceptions").value;
	var array = exc.split("\n");
	storage.set("notificationexceptions", array);
	console.log("Exceptions: " , storage.get("notificationexceptions"));
}

/** Names pages that won't have any actions taken for them. */
function setExceptions() {
	var exc = document.getElementById("Exceptions").value;
	var array = exc.split("\n");
	storage.set("exceptions", array);
	console.log("Exceptions: " + storage.get("exceptions"));
}

/** Names links to not change, regardless of where they are. */
function setLinkExceptions() {
	var exc = document.getElementById("LinkExceptions").value;
	var array = exc.split("\n");
	storage.set("linkexceptions", array);
	console.log("Link Exceptions: " + storage.get("linkexceptions"));
}

document.getElementById("ccoe").addEventListener('click', clearCache);

/** Toggles the clearing of all statistics on extension exit */
function clearCache() {
	storage.clearCerts();
}

storage.getTotal(document.getElementById("Fixes"));
storage.getUniques(document.getElementById("UFixes"));
storage.getSuccessRate(document.getElementById("Success"));
